#include <iostream>
using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2, int carry = 0) {
        if (l1 == nullptr && l2 == nullptr && carry == 0)
            return nullptr;

        int s = carry;
        if (l1) {
            s += l1->val;
            l1 = l1->next;
        }
        if (l2) {
            s += l2->val;
            l2 = l2->next;
        }

        return new ListNode(s % 10, addTwoNumbers(l1, l2, s / 10));
    }
};

// 辅助函数：创建链表
ListNode* createList(const int arr[], int size) {
    if (size == 0) return nullptr;
    ListNode* head = new ListNode(arr[0]);
    ListNode* cur = head;
    for (int i = 1; i < size; ++i) {
        cur->next = new ListNode(arr[i]);
        cur = cur->next;
    }
    return head;
}

// 辅助函数：打印链表
void printList(ListNode* head) {
    while (head != nullptr) {
        cout << head->val << " -> ";
        head = head->next;
    }
    cout << "nullptr" << endl;
}

int main() {
    // 测试用例1
    int a1[] = {2, 4, 3}; // 代表 342
    int a2[] = {5, 6, 4}; // 代表 465
    ListNode* l1 = createList(a1, 3);
    ListNode* l2 = createList(a2, 3);

    Solution sol;
    ListNode* res1 = sol.addTwoNumbers(l1, l2);
    cout << "Test Case 1: ";
    printList(res1); // 应该输出 7 -> 0 -> 8 -> nullptr
    return 0;
}